package com.d2.upload.service;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectResult;
import com.d2.upload.entity.AmazonFileModel;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


@Service
public class AmazonServiceImpl implements AmazonService {
    /** bucket */
    final String bucketName = "wozhishi";
    /** accessKey */
    final String accessKey = "AKIA6O2JY3TMJKE73YND";
    /** secretKey */
    final String secretKey = "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";
    /** endpoint */
    final String endpoint = "https://s3.ap-northeast-1.amazonaws.com";
    /** aws s3 client */
    AmazonS3 s3 = null;
    @Override
    public AmazonFileModel upload(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
//        int beginIndex = originalFilename.lastIndexOf(".");
//        String ext = (originalFilename.substring(beginIndex));

        String contentType = file.getContentType();
        long fileSize = file.getSize();
        String dateDir = new SimpleDateFormat("/yyyy/MM/dd").format(new Date());
        String tempBucketName = bucketName+dateDir;
        String filePath = dateDir + "/" + originalFilename;
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(contentType);
        objectMetadata.setContentLength(fileSize);
        try {
            PutObjectResult putObjectResult = s3.putObject(tempBucketName, originalFilename, file.getInputStream(), objectMetadata);
        } catch (IOException e) {
            e.printStackTrace();
        }
        AmazonFileModel amazonFileModel = new AmazonFileModel ();
        amazonFileModel.setFileName(originalFilename);
        amazonFileModel.setFileSize(fileSize);
        amazonFileModel.setFileType(contentType);
        amazonFileModel.setFilePath(filePath);
        amazonFileModel.setUrl("https://wozhishi.s3-ap-northeast-1.amazonaws.com" + filePath);
        return amazonFileModel ;
    }

    @PostConstruct
    public void init(){
        ClientConfiguration config = new ClientConfiguration();
        AwsClientBuilder.EndpointConfiguration endpointConfig =
                new AwsClientBuilder.EndpointConfiguration(endpoint, "ap-northeast-1");

        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey,secretKey);
        AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
        s3  = AmazonS3Client.builder()
                .withEndpointConfiguration(endpointConfig)
                .withClientConfiguration(config)
                .withCredentials(awsCredentialsProvider)
                .disableChunkedEncoding()
                .withPathStyleAccessEnabled(true)
                .build();
    }
}
